package com.zqp.web.anync;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;


/**
* @author zqp
* @date 2017年12月19日 
* @description 
*/
//spring 容器 启动完毕的事件
@Component
public class QueueListener implements ApplicationListener<ContextRefreshedEvent> {
	
	private Logger logger = LoggerFactory.getLogger(getClass());
	@Autowired
	private MockQueue mockQueue;
	@Autowired
	private DeferredResultHolder deferredResultHolder;
	
	
	@Override
	public void onApplicationEvent(ContextRefreshedEvent event) {
		
		
		new Thread(()->{
			//放到一个单独的线程里..
			while(true) {
				if(StringUtils.isNotBlank(mockQueue.getComplateOrder())) {
						String orderNumer = mockQueue.getComplateOrder();
						logger.info("返回订单处理结果" + orderNumer);
						deferredResultHolder.getMap().get(orderNumer).setResult("place order success");
						mockQueue.setComplateOrder(null);
				}else {
					try {
						Thread.sleep(100);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}	
			
			
		}).start(); ;
		
	
	}
}
